var time_ticker = function (txt) {

    this.txt = txt;
    this.playingSecond = 0;
    this.totalSecond = 0;
    this.timeInterval = null;

    time_ticker.prototype.setPlayingSecond = function (v) {
        this.playingSecond = v;
    }

    time_ticker.prototype.setTotalSecond = function (v) {
        this.totalSecond = v;
        this.totalSecondFormat = this._format_time(this.totalSecond);
    }

    time_ticker.prototype.start = function () {
        this.startSecond = parseInt(new Date().getTime() / 1000);

        this.stop();

        var self = this;
        this.timeInterval = setInterval(function () {
            self._refresh(self);
        }, 1000);
    };

    time_ticker.prototype.stop = function () {
        if (this.timeInterval != null) {
            clearInterval(this.timeInterval);
            this.timeInterval = null;
        }

        this.txt.innerText = this._format_time(this.playingSecond) + " / " + this.totalSecondFormat;
    };

    time_ticker.prototype._format_value = function (v) {
        if (v < 10) {
            return "0" + v;
        } else {
            return v;
        }
    };

    time_ticker.prototype._format_time = function (sec) {
        var s = sec % 60;
        var m = parseInt(sec / 60) % 60;
        var h = parseInt(sec / 3600) % 60;

        var func = this._format_value;
        return func(h) + ":" + func(m) + ":" + func(s);
    };

    time_ticker.prototype._refresh = function (self) {
        var now = parseInt(new Date().getTime() / 1000);
        var showTime = self.playingSecond + (now - self.startSecond);
        if (showTime >= self.totalSecond) {
            showTime = self.totalSecond;
            if (self.timeInterval != null) {
                clearInterval(self.timeInterval);
            }
        }

        this.txt.innerText = self._format_time(showTime) + " / " + self.totalSecondFormat;
    };
}